<template>
  <div>
    <div class="app-container">
      <Card>
        <p slot="title">退货单详情</p>
        <div class="head">
          <div class="head-box">
            <div class="box-item">退货单号：{{ goodsItem.scmRefundId }}</div>
            <div class="box-item">客户名称：{{ goodsItem.custName }}</div>
            <div class="box-item">退货总数量：{{ goodsItem.refundQty }}</div>
          </div>
          <div class="head-box">
            <div class="box-item">总金额(元)：￥{{ goodsItem.refundAmt }}</div>
            <div class="box-item">
              创建时间：{{ this.$date(goodsItem.postTime,'yyyy-MM-dd HH:mm:ss') }}
            </div>
            <div class="box-item">备注：{{ goodsItem.refundNotes }}</div>
          </div>
        </div>
      </Card>
      <Card style="margin-top: 16px">
        <p slot="title">商品明细</p>
        <div>
          <Table border :columns="columnsSub" :data="dataGrid"></Table>
        </div>
        <div class="footer-btn">
          <Button @click="Submit" type="primary">一键入库</Button>
        </div>
      </Card>
    </div>
  </div>
</template>
<script>
export default {
  name: "create",
  data() {
    return {
      vueKey: 0,
      showLocation:false,
      goodsIndex: 0,
      rowItem: {},
      shelfSkuList: [],
      item: {},
      dataGrid: [],
      count: 0,
      query: {
        pageIndex: 1,
        pageSize: 1000,
        equal: {},
      },
      columnsSub: [
        {
          title: "商品图片",
          key: "goodsImg",
          width: 100,
          render: (h, params) => {
            return h("div", [
              h(
                "img",
                {
                  attrs: {
                    src: params.row.goodsImg,
                  },
                  style: {
                    width: "50px",
                    height: "46px",
                  },
                },
                "View"
              ),
            ]);
          },
        },
        {
          title: "商品名称",
          key: "goodsName",
        },
        {
          title: "商品条码",
          key: "eanCode",
        },
        {
          title: "系统进价(元)",
          key: "systemPrice",
          sortable: true,
          width: 150,
          render: (h, params) => {
            return h("div", [
              h("a", { style: { fontSize: "12px", color: "gray" } }, "￥"),

              h("span", params.row.systemPrice.toFixed(2)),
            ]);
          },
        },
        {
          title: this.$store.getUser('userType') === 9 ? '采购数量' : '要货数量',
          key: "goodsQty",
          width: 110
        },
        {
          title: "入库数量",
          key: "goodsQty",
          width: 150,
          render: (h, params) => {
            return h("InputNumber", {
              props: {
                min: 0,
                max: params.row.goodsQty,
                value: params.row.goodsQty,
                size: "small",
              },
              on: {
                input: (val) => {
                  let list = [{
                    total: Number(val)
                  }]
                  this.dataGrid[params.index].shelfSkuList = list
                }
              }
            })
          },
        }
      ],
    };
  },
  props: {
    goodsItem: Object,
    callbank: Function
  },
  mounted() {
  },
  methods: {
    Submit() {
      let formlist = []
      /* let bool = false
      this.dataGrid.forEach((item) => {
        if (!item.shelfSkuList) {
          bool = true
          return
        }
      })
      if(bool) {
        this.$Message.warning("请选择入库货位")
        return
      } */
      let orderTotal = 0
      this.dataGrid.forEach((item) => {
        let sku_qty = 0
        if (item.shelfSkuList) {
          item.shelfSkuList.forEach((ob) => {
            sku_qty += ob.total
          })
        } else {
          sku_qty = item.goodsQty
        }
        
        orderTotal += sku_qty
        let formData = {
          isbType: 2, //2退货入库
          refundOrderId: this.goodsItem.scmRefundId,
          goodsOrderId: this.goodsItem.shopOrderId,
          orderDetailId: item.scmRefundDetailId,
          imageUrl: item.skuImage,
          systemPrice: item.systemPrice,
          skuId: item.skuId,
          isbQty: sku_qty, //实际入库数量（签收数量）
          eanCode: item.eanCode,
          skuName: item.goodsName,
          skuUom:  item.skuUom,
          storeId: this.$store.getWms('warehouseId'),
          storeName: this.$store.getWms("warehouseName"),
          regionCode: this.$store.getWms('countyCode'),
          shelfSkuList: item.shelfSkuList,
        }
        formlist.push(formData)
      })
      if(orderTotal <= 0) {
        this.$Message.warning("总入库数量不能小于等于零")
        return
      }
      this.doInstock(formlist,orderTotal)
    },
    doInstock(formlist,orderTotal) {
      this.$Modal.confirm({
        title: "一键入库",
        content: `入库总数量为【${orderTotal}】，确认入库后不可撤消`,
        width: 416,
        onOk: () => {
          new this.$rest({
            gateway: 'agroerp',
            service: "agroerp-wms",
            url: "/v1.0/plus/smart/instock",
            data: {
              pojo: formlist,
            },
            action: (res) => {
              if (res.data.code === 20000) {
                this.$Message.success("入库成功")
                this.callbank()
              } else {
                this.$Notice.warning({
                  title: "操作失败",
                  desc: `失败原因：${res.data.msg}`,
                });
              }
            }
          }).post()
        }
      })
    },
    init(id) {
      this.query.equal.scmRefundId = id
      new this.$rest({
        gateway: 'agrobiz',
        service: "agrobiz-order",
        url: "/v1.0/mall/refund/order/detail/page",
        query: this.query,
        action: (res) => {
          if (res.data.code === 20000) {
            const data = res.data.data.list;
            this.dataGrid = data;
            this.count = res.data.data.count;
          } else {
            this.$Notice.warning({
              title: "获取失败",
              desc: `失败原因：${res.data.msg}`,
            });
          }
        },
      }).get();
    },
  }
};
</script>
<style lang="less" scoped>
.head {
  width: 100%;
}
.head-title {
  font-size: 20px;
  height: 40px;
  font-weight: bold;
}
.head-box {
  display: flex;
  width: 100%;
  justify-content: space-between;
  height: 50px;
}
.box-item {
  width: 30%;
  display: flex;
  align-items: flex-start;
}
.btns {
  margin: 16px 0px;
}
.footer {
  display: flex;
  justify-content: end;
  margin-top: 25px;
}
.footer-btn {
  display: flex;
  justify-content: end;
  margin-top: 25px;
}
</style>